package com.cy.pj.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.CheckBox;
import com.cy.pj.common.vo.PageObject;
import com.cy.pj.common.vo.SysRoleMenuVo;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.entity.SysRole;
import com.cy.pj.sys.service.SysRoleService;

import io.micrometer.core.instrument.util.StringUtils;

@Service
public class SysRoleServiceImpl implements SysRoleService{
	
	@Autowired
	private SysRoleDao dao;
	@Autowired
	private SysRoleMenuDao srmd;
	@Autowired
	private SysUserRoleDao surd;
	
	@Override
	public PageObject<SysRole> findPageObject(String name, Integer pageCurrent) {
		//1.验证参数合法性
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("页码值无效");
		//2.查询记录总数并验证
		int rowCount = dao.getRowCount(name);
		if(rowCount==0)
			throw new ServiceException("没有对应的记录");
		int pageSize=3;
//		int pageCount=rowCount/pageSize;
//		if(rowCount%pageSize!=0) {
//			pageCount++;
//		}
//		if(pageCurrent>pageCount) {
//			throw new ServiceException("页码值不能大于总页数");
//		}
		//3.查询当前页记录
		int startIndex=(pageCurrent-1)*pageSize;
		List<SysRole> findPageObjects = dao.findPageObjects(name, startIndex, pageSize);
		//4.封装结果并返回
		return new PageObject<>(pageCurrent, pageSize, rowCount, findPageObjects);
	}

	@Override
	public int deleteObject(Integer id) {
		//1.验证数据的合法性
		if(id==null||id<0) {
			throw new IllegalArgumentException("请选择");
		}
		//2.基于id删除关系数据
		srmd.deleteObjectsByRoleId(id);
		surd.deleteObjectsByRoleId(id);
		//3.删除角色自身id
		int rows = dao.deleteObject(id);
		if(rows==0) {
			throw new ServiceException("此菜单可能已经不存在");
		}
		return rows;
	}

	@Override
	public int saveObject(SysRole entity, Integer[] menuIds) {
		//1.合法性验证
		if(entity==null) {
			throw new ServiceException("保存数据不能为空");
		}
		if(StringUtils.isEmpty(entity.getName())) {
			throw new ServiceException("角色不能为空");
		}
		if(menuIds==null||menuIds.length==0) {
			throw new ServiceException("必须为角色赋予权限");
		}
		//2.保存数据
		int rows = dao.insertObject(entity);
		srmd.insertObjects(entity.getId(), menuIds);
		//3.返回结果
		return rows;
	}

	@Override
	public SysRoleMenuVo findObjectById(Integer id) {
		//1.合法性验证
    	if(id==null||id<=0)
    	throw new ServiceException("id的值不合法");
    	//2.执行查询
    	SysRoleMenuVo result=dao.findObjectById(id);
    	//3.验证结果并返回
    	if(result==null)
    	throw new ServiceException("此记录已经不存在");
    	return result;
	}

	@Override
	public int updateObject(SysRole entity, Integer[] menuIds) {
		//1.合法性验证
    	if(entity==null)
        throw new ServiceException("更新的对象不能为空");
    	if(entity.getId()==null)
    	throw new ServiceException("id的值不能为空");
    	
    	if(StringUtils.isEmpty(entity.getName()))
    	throw new ServiceException("角色名不能为空");
    	if(menuIds==null||menuIds.length==0)
    	throw new ServiceException("必须为角色指定一个权限");
    	
    	//2.更新数据
    	int rows=dao.updateObject(entity);
    	if(rows==0)
        throw new ServiceException("对象可能已经不存在");
    	srmd.deleteObjectsByRoleId(entity.getId());
    	srmd.insertObjects(entity.getId(),menuIds);
    	//3.返回结果
    	return rows;
	}

	@Override
	public List<CheckBox> findObjects() {
		return dao.findObjects();
	}

}










